/*
 * Copyright (c) 2009.
 */

package kz.home.db;

import kz.home.objects.Movie;

import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;

/**
 * мой пример
 * User: danik
 * Date: 14.07.2009
 * Time: 0:32:58
 */
public class MyTestStore extends MIDlet {
    private static final String DBNAME = "movies";


    protected void startApp() throws MIDletStateChangeException {
        Movie movie = new Movie("The Matrix", "123", "norm");
        Movie movie2 = new Movie("Ледниковый период 3", "124", "norm");
        RecordStore rs;

        try {
            rs = RecordStore.openRecordStore(DBNAME, true);
            writeToDB(movie, rs);
            rs.closeRecordStore();

            System.out.println("stored");
            Movie fromMovie = readFromStore();
            System.out.println("name from DB[" + fromMovie.getName() + "]");
        } catch (Exception e) {
            // ignore any errors...
            e.printStackTrace();
        }
    }

    private void writeToDB(Movie movie, RecordStore rs) throws Exception{
        byte[] outputRecord;
        String outputString = "First Record";
        int outputInteger = 15;
        boolean outputBoolean = true;

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        DataOutputStream outputDataStream = new DataOutputStream(outputStream);

        /*outputDataStream.writeUTF(movie.getName());
        outputDataStream.writeUTF(movie.getUrl());
        outputDataStream.writeUTF(movie.getState());*/

        outputDataStream.writeUTF(outputString);
        outputDataStream.writeBoolean(outputBoolean);
        outputDataStream.writeInt(outputInteger);

        outputDataStream.flush();

        outputRecord = outputStream.toByteArray();
        rs.addRecord(outputRecord, 0, outputRecord.length);

        outputStream.reset();
        outputStream.close();
        outputDataStream.close();
       // printArray(outputRecord);
    }

    private Movie readFromStore() {
        Movie movie = null;
        try {
            RecordStore rs = RecordStore.openRecordStore(DBNAME, false);
            String inputString = null;
            int inputInteger = 0;
            boolean inputBoolean = false;
            byte[] byteInputData = new byte[100];

            ByteArrayInputStream inputStream=null;

            DataInputStream inputDataStream = null;
             
            for (int x = 1; x < rs.getNumRecords(); x++) {
                rs.getRecord(x, byteInputData, 0);
                //  printArray(byteInputData);
                //  System.out.println("read[" + inputDataStream.readUTF() + "]");
                //movie = new Movie(inputDataStream.readUTF(), inputDataStream.readUTF(), inputDataStream.readUTF());
                inputStream = new ByteArrayInputStream(byteInputData);
                inputDataStream = new DataInputStream(inputStream);

                inputString = inputDataStream.readUTF();
                inputBoolean = inputDataStream.readBoolean();
                inputInteger = inputDataStream.readInt();

                System.out.println("str[" + inputString + "]");
                System.out.println("bool[" + inputBoolean + "]");
                System.out.println("int[" + inputInteger + "]");
                inputStream.reset();
            }

            if (inputStream != null) inputStream.close();            
            if (inputDataStream!= null) inputDataStream.close();
            if (rs != null) rs.closeRecordStore();
        } catch (Exception error) {
            error.printStackTrace();
        }

        return movie;
    }

    protected void pauseApp() {

    }

    protected void destroyApp(boolean b) throws MIDletStateChangeException {

    }

    protected void printArray(byte[] arr) {
        if (arr != null) {
            for (int i = 0; i < arr.length; i++) {
                System.out.print(arr[i] + " ");
            }
        }

        System.out.println(" ----------- ");
    }
}
